package com.example.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    // 定义交换机名称
    public static final String MAIN_EXCHANGE = "main-exchange";
    public static final String FORWARDING_EXCHANGE = "forwarding-exchange";
    
    // 定义队列名称
    public static final String USER_QUEUE = "user-queue";
    public static final String ORDER_QUEUE = "order-queue";
    public static final String NOTIFICATION_QUEUE = "notification-queue";
    public static final String ANALYTICS_QUEUE = "analytics-queue";
    
    // 定义路由键
    public static final String USER_ROUTING_KEY = "event.user";
    public static final String ORDER_ROUTING_KEY = "event.order";
    public static final String NOTIFICATION_ROUTING_KEY = "event.notification";
    public static final String ALL_EVENTS_ROUTING_KEY = "event.#";

    // 创建主交换机 - Topic类型，根据路由键模式匹配转发消息
    @Bean
    public TopicExchange mainExchange() {
        return new TopicExchange(MAIN_EXCHANGE);
    }
    
    // 创建转发交换机 - Fanout类型，用于广播消息到所有绑定的队列
    @Bean
    public FanoutExchange forwardingExchange() {
        return new FanoutExchange(FORWARDING_EXCHANGE);
    }
    
    // 创建用户队列
    @Bean
    public Queue userQueue() {
        return new Queue(USER_QUEUE);
    }
    
    // 创建订单队列
    @Bean
    public Queue orderQueue() {
        return new Queue(ORDER_QUEUE);
    }
    
    // 创建通知队列
    @Bean
    public Queue notificationQueue() {
        return new Queue(NOTIFICATION_QUEUE);
    }
    
    // 创建分析队列 - 用于事件转发
    @Bean
    public Queue analyticsQueue() {
        return new Queue(ANALYTICS_QUEUE);
    }
    
    // 绑定用户队列到主交换机
    @Bean
    public Binding userBinding(Queue userQueue, TopicExchange mainExchange) {
        return BindingBuilder.bind(userQueue).to(mainExchange).with(USER_ROUTING_KEY);
    }
    
    // 绑定订单队列到主交换机
    @Bean
    public Binding orderBinding(Queue orderQueue, TopicExchange mainExchange) {
        return BindingBuilder.bind(orderQueue).to(mainExchange).with(ORDER_ROUTING_KEY);
    }
    
    // 绑定通知队列到主交换机
    @Bean
    public Binding notificationBinding(Queue notificationQueue, TopicExchange mainExchange) {
        return BindingBuilder.bind(notificationQueue).to(mainExchange).with(NOTIFICATION_ROUTING_KEY);
    }
    
    // 绑定分析队列到转发交换机
    @Bean
    public Binding analyticsBinding(Queue analyticsQueue, FanoutExchange forwardingExchange) {
        return BindingBuilder.bind(analyticsQueue).to(forwardingExchange);
    }
    
    // 绑定主交换机到转发交换机 - 实现事件转发
    @Bean
    public Binding forwardBinding(FanoutExchange forwardingExchange, TopicExchange mainExchange) {
        return BindingBuilder.bind(forwardingExchange).to(mainExchange).with(ALL_EVENTS_ROUTING_KEY);
    }
    
    // 配置RabbitTemplate，使用注入的消息转换器
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, Jackson2JsonMessageConverter messageConverter) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter);
        return rabbitTemplate;
    }
} 